// pages/my/index.js
const app = getApp(); // 获取应用实例

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bomMenus: [], //底部菜单数据
    bomMenuId: 4, //页面ID
    bomMenuKd: '19%', //菜单宽度
    xcx_shop: 1, //审核商业：0无，1有
    uname: '', uhead: '', //用户信息
    //用户统计信息
    family: [],
    myGoodsTotal: 0, myFuwuTotal: 0, myAddressTotal: 0,
    myOrderGoods: 0, myOrderFuwu: 0,
    myGorderList: [], myFworderList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.showUserInfo();
    this.bomMenuList();
    this.tongji();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 底部菜单
  bomMenuList: function () {
    // this.setData({ bomMenus: app.globalData.bomAry }); return
    var _this = this;
    wx.request({
      method: 'post',
      url: app.globalData.xcxApi + 'init',
      data: {},
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        console.log(res.data);
        res = res.data;
        if (res.code < 0) {
          return
        }
        _this.setData({
          bomMenus: res.data.menus,
          bomMenuKd: res.data.kd,
          xcx_shop: res.data.menus.length>3 ? 1 : 0,
        });
      },
      error: function (err) {
        console.log(err);
      }
    })
  },

  // 页面跳转
  toView: function(e) {
    console.log(e);
    var id = e.currentTarget.dataset.id;
    if (id == this.data.bomMenuId) { return }
    var bms = this.data.bomMenus, mlink = '', i = 0;
    for (; i < bms.length; i ++) {
      if (bms[i].id == id) {
        mlink = bms[i].link;
      }
    }
    console.log(mlink);
    // wx.navigateBack({
    //   url: mlink,
    // });
    wx.navigateTo({
      url: mlink,
    });
  },

  // 我的信息
  showUserInfo: function (e) {
    var uinfo = wx.getStorageSync('uinfo');
    console.log(uinfo);
    if (!uinfo) {
      wx.showToast({
        title: '没有登录授权', icon: 'none',
      }); return
    }
    this.setData({
      uname: uinfo.nick,
      uhead: uinfo.head,
    });
  },
  
  // 用户统计
  tongji: function (e) {
    var uinfo = wx.getStorageSync('uinfo');
    if (!uinfo) {
      wx.showToast({
        title: '没有登录', icon: 'none',
      }); return
    }
    var _this = this;
    wx.request({
      method: 'post',
      url: app.globalData.xcxApi + 'my/tongji',
      data: { uid: uinfo.uid },
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        console.log(res.data);
        res = res.data;
        if (res.code < 0) {
          return
        }
        _this.setData({
          family: res.data.family,
          myAddressTotal: res.data.addressTotal,
          myGoodsTotal: res.data.goodsTotal,
          myFuwuTotal: res.data.fuwuTotal,
          myOrderGoods: res.data.orderShop,
          myOrderFuwu: res.data.orderFuwu,
          myGorderList: res.data.orderShopList,
          myFworderList: res.data.orderFuwuList,
        });
      },
      error: function (err) {
        console.log(err);
      }
    });
  },

  // 去我的信息
  toMyinfo: function () {
    wx.navigateTo({
      url: '/pages/index/wxlogin/index',
    });
  },

  // 去收货地址
  toAddress: function () {
    wx.navigateTo({
      url: '/pages/my/address/index',
    });
  },

  // 去店铺
  toShop: function () {
    wx.navigateTo({
      url: '/pages/my/shop/index',
    });
  },

  // 去产品
  toGoods: function () {
    wx.navigateTo({
      url: '/pages/my/goods/index',
    });
  },

  // 去便民
  toFuwu: function () {
    wx.navigateTo({
      url: '/pages/my/fuwu/index',
    });
  },

  // 去商品订单
  toOrderShop: function () {
    wx.navigateTo({
      url: '/pages/my/gorder/index',
    });
  },

  // 去服务订单
  toOrderFuwu: function () {
    wx.navigateTo({
      url: '/pages/my/fworder/index',
    });
  },

  // 我的推广
  toAdList: function () {
    wx.navigateTo({
      url: '/pages/my/adlist/index',
    });
  },
})